package Part2_java.util.Map.HashMap;
import org.junit.Test;
import java.util.*;

/**
 * ClassName: Demo
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.04.14 18:54
 */

public class Demo {
    @Test
    public void test1(){

        HashMap<Object, Object> map = new HashMap<>();

        map.put("AA",56);
        map.put("BB",78);
        map.put(67,"Tom");
        map.put(new User("$",23),45);
        map.put(new User("$",23),45);//User重写了hashCode方法

        System.out.println(map);
        System.out.println(map.size());//4
    }

    @Test
    public void test2(){

        HashMap<Object, Object> map = new HashMap<>();

        map.put("AA",56);
        map.put("BB",78);
        map.put(67,"Tom");
        map.put(new User("$",23),45);

        //keySet方法map中的Key的Set集合取出来了
        Set<Object> keySet = map.keySet();
        //遍历Key集
        Iterator iterator1 = keySet.iterator();
        while (iterator1.hasNext()){
            Object key = iterator1.next();
            System.out.println(key.toString());
            //System.out.println(iterator.next());
        }

        System.out.println("********************");

        //遍历value集
        Collection values = map.values();
        for(Object o : values){
            System.out.println(o);
        }

    }

    @Test
    public void test3(){
        HashMap<Object, Object> map = new HashMap<>();

        map.put("AA",56);
        map.put("BB",78);
        map.put(67,"Tom");
        map.put(new User("$",23),45);//生成一次hashCode
        map.put(new User("$",23),45);//再生成一次hashCode，并调用equals方法比较

        Set entry = map.entrySet();
        /*for(Object o : entry){
            System.out.println(o);
        }*/
        Iterator iterator = entry.iterator();
        while (iterator.hasNext()){
            Map.Entry next = (Map.Entry) iterator.next();//这里的Entry接口是内部接口，需要Map.Entry
            System.out.println(next.getKey()+"---->"+next.getValue());
            //简单方式，相当于调用父类的toString方法
            //System.out.println(iterator.next());
            //System.out.println(iterator.next().toString());
        }
    }

}


